package com.cl.ks.service;


import com.alibaba.fastjson.JSONArray;
import com.cl.ks.entity.KsDataField;
import com.cl.ks.repository.KsDataFieldRepository;
import com.cl.mvc.service.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class KsDataFieldService extends BaseService<KsDataField> {
    protected KsDataFieldService(KsDataFieldRepository KsDataFieldRepository) {
        super(KsDataFieldRepository);
    }

    public void importKsDataTypeField(KsDataField ksDataField) {
        ksDataField.setId(null);
        KsDataField param  = new KsDataField() ;
        param.setCode(ksDataField.getCode());
        param.setDataTypeCode(ksDataField.getDataTypeCode());
        Optional<KsDataField> entity = findByEntity(param);
        if (!entity.isPresent()) {
            repository.save(ksDataField);
        }

    }

    public void importKsDataTypeFields(List<KsDataField> ksDataTypeList) {
        for (KsDataField ksDataTypeField : ksDataTypeList) {
            importKsDataTypeField(ksDataTypeField);
        }
    }

    public JSONArray exportKsDataFields(List<String> ksDataTypeCodes) {

        JSONArray res = new JSONArray();
        for (String ksDataTypeCode : ksDataTypeCodes) {
            KsDataField param = new KsDataField();
            param.setDataTypeCode(ksDataTypeCode);
            param.setRuleFieldFlag(null);
            List<KsDataField> ksDataTypeOptional = findAllByEntity(param);
            if (ksDataTypeOptional.isEmpty()) {
                continue;
            }
//            KsDataField ksDataType = ksDataTypeOptional.get();
//            List<KsDataField> ksDataFieldList = ksDataType.getKsDataFieldList();
            res.addAll(ksDataTypeOptional);
        }
        return res;
    }
}
